﻿<?php
defined('BASEPATH') or exit('No direct script access allowed');

class ProjectInfo extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model("Project_model");
        $this->load->model("Vertical_project_model");
        $this->load->model("Horizontal_project_model");
    }

    //保存项目详情经费项目同步到课题名称表
    public function saveProjectData()
    {
        $patentInfo = $this->load->database('project', true);
        $sql        = "select db_project.*,db_admin.username from db_project left join db_admin on db_admin.id=db_project.admin_id where db_project.status=1 and official=0";
        $data1      = $patentInfo->query($sql)->result_array();
        // var_dump($data1);
        if ($data1) {
            $id = array();
            foreach ($data1 as $k => $v) {
                if($v['username']){
                    $connar['where'] = $this->sqlEscape('name=?? and type=?? and admin=?? and from=?? and date=??  ', array($v['name'], $v['type'], $v['username'], $v['project_dept'], $v['project_approval']));
                    //去掉重复的数据
                    $result = $this->Project_model->get_list_full($connar);
                    // var_dump($result);
                    if (!$result) {
                        //添加
                        $param['name']   = $v['name'];
                        $param['type']   = $v['type'];
                        $param['admin']  = $v['username'];
                        $param['from']   = $v['project_dept'];
                        $param['date']   = $v['project_approval'];
                        $param['is_end'] = $v['next_part'];
    
                        $param['created'] = time();
                        $id[]             = $this->Project_model->add($param);
                    }
                }
               
            }

            exit("成功插入" . count($id) . "条数据");
        }

    }


    //成果里同步横向成果到课题名称表
    public function saveProjectDataCghx(){
        //横向课题名称
        $connar['items'] ="db_horizontal_project.id,db_horizontal_project.name,db_horizontal_project.number,db_horizontal_project.source,db_horizontal_project.is_end,db_horizontal_project.project_time,author.head_name,author.authorcode,author.identity";
        $connar['join'][0]=array('(select group_concat(db_author.author_name  ORDER BY db_author.id ASC) head_name,group_concat(db_user.usercode ORDER BY db_author.id ASC ) authorcode,group_concat(db_author.identity  ORDER BY db_author.id ASC) identity,data_id FROM db_author left join db_user on db_user.id = db_author.author_id WHERE db_author.type = '.ResultsType::Eighth[XPHEnum::Value].' and db_user.is_del = 0  GROUP BY db_author.data_id ) author','author.data_id=db_horizontal_project.id');
        $connar['where'] = $this->sqlEscape('db_horizontal_project.is_del=??', array(0));
        $connar['groupby'] = 'name';

        $data1 = $this->Horizontal_project_model->get_list_full($connar);

        if ($data1) {
            $id = array();
            foreach ($data1 as $k => $v) {
                if($v['head_name']){
                    $connar1['where'] = $this->sqlEscape('name=?? and type=?? and admin=?? and from=?? and date=??  ', array($v['name'], 1, $v['head_name'], $v['source'], $v['project_time']));
                    //去掉重复的数据
                    $result = $this->Project_model->get_list_full($connar1);
                    if (!$result) {
                        //添加
                        $param['name']   = $v['name'];
                        $param['type']   = 1;
                        $param['admin']  = $v['head_name'];
                        $param['from']   = $v['source'];
                        $param['date']   = $v['project_time'];
                        $param['is_end'] = $v['is_end'];
                        $param['kt_number'] = $v['number'];
                        $param['source'] = '成果';
                        $param['created'] = time();
                        $id[]             = $this->Project_model->add($param);
                    }
                }
               
            }

            exit("成功插入" . count($id) . "条数据");
        }
    }

    //成果里同步纵向课题到课题名称表
    public function saveProjectDataCgzx(){
          //纵向课题名称
          $connar['items'] ="db_vertical_project.id,db_vertical_project.name,db_vertical_project.number,db_vertical_project.source,db_vertical_project.is_end,db_vertical_project.project_time,author.head_name,author.authorcode,author.identity";
          $connar['join'][0]=array('(select group_concat(db_author.author_name  ORDER BY db_author.id ASC) head_name,group_concat(db_user.usercode ORDER BY db_author.id ASC ) authorcode,group_concat(db_author.identity  ORDER BY db_author.id ASC) identity,data_id FROM db_author left join db_user on db_user.id = db_author.author_id WHERE db_author.type = '.ResultsType::Seventh[XPHEnum::Value].' and db_user.is_del = 0  GROUP BY db_author.data_id ) author','author.data_id=db_vertical_project.id');
          $connar['where'] = $this->sqlEscape('db_vertical_project.is_del=??', array(0));
          $connar['groupby'] = 'name';
  
          $data1 = $this->Vertical_project_model->get_list_full($connar);
        //   echo $this->db->last_query();
  
          if ($data1) {
              $id = array();
              foreach ($data1 as $k => $v) {
                  if($v['head_name']){
                      $connar1['where'] = $this->sqlEscape('name=?? and type=?? and admin=?? and from=?? and date=??  ', array($v['name'], 2, $v['head_name'], $v['source'], $v['project_time']));
                      //去掉重复的数据
                      $result = $this->Project_model->get_list_full($connar1);
                      if (!$result) {
                          //添加
                          $param['name']   = $v['name'];
                          $param['type']   = 2;
                          $param['admin']  = $v['head_name'];
                          $param['from']   = $v['source'];
                          $param['date']   = $v['project_time'];
                          $param['is_end'] = $v['is_end'];
                          $param['kt_number'] = $v['number'];
                          $param['source'] = '成果';
                          $param['created'] = time();
                          $id[]             = $this->Project_model->add($param);
                      }
                  }
                 
              }
  
              exit("成功插入" . count($id) . "条数据");
          }
    }
}
